package com.cn.unarmed.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.cn.unarmed.adapter.MyGridRecyclerViewAdapter;
import com.cn.unarmed.adapter.MyRecyclerViewDirection;
import com.cn.unarmed.base.BaseActivity;
import com.cn.unarmed.bean.GridDataModle;
import com.cn.unarmed.commonutils.AppUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * http://blog.csdn.net/juhua2012/article/details/52038421,adapter的包以及依赖类出自于这个github项目
 * 快速适配器
 */
public class ViewActivity0630 extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view0630);

        initView();
        initData();
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        if (component.equals(TitleBar.LEFT)) {
            finish();
        }
    }


    private RecyclerView myRecyclerView1, myRecyclerView2;
    private TextView textView1, textView2;

    private List<GridDataModle> list1, list2;//数据源
    private List<Object> list;

    private GridLayoutManager getGridLayoutManager1, getGridLayoutManager2;

    private void initView() {
        textView1 = (TextView) this.findViewById(R.id.text_grid1);
        textView2 = (TextView) this.findViewById(R.id.text_grid2);
        myRecyclerView1 = (RecyclerView) this.findViewById(R.id.recyclerView1);
        myRecyclerView2 = (RecyclerView) this.findViewById(R.id.recyclerView2);
        getGridLayoutManager1 = new GridLayoutManager(this, 6);
        getGridLayoutManager2 = new GridLayoutManager(this, 6);
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        list1 = getNewList(list);
        for (GridDataModle gridDataModle:list1){
            gridDataModle.setType_lay(1);
        }
        list2 = getNewList(list);
        for (GridDataModle gridDataModle:list2){
            gridDataModle.setType_lay(2);
        }

        getGridLayoutManager1.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return list1.get(position).getType();
            }
        });

        getGridLayoutManager2.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return list2.get(position).getType();
            }
        });

        myRecyclerView1.setLayoutManager(getGridLayoutManager1);
        myRecyclerView1.addItemDecoration(new MyRecyclerViewDirection(this,list1.size()));

        myRecyclerView2.setLayoutManager(getGridLayoutManager2);
        myRecyclerView2.addItemDecoration(new MyRecyclerViewDirection(this,list2.size()));


        MyGridRecyclerViewAdapter adapter1 = new MyGridRecyclerViewAdapter(this,list1);
        MyGridRecyclerViewAdapter adapter2 = new MyGridRecyclerViewAdapter(this,list2);
        myRecyclerView1.setAdapter(adapter1);
        myRecyclerView2.setAdapter(adapter2);
        adapter1.setItemClickListener(new MyGridRecyclerViewAdapter.ItemClickLisener() {
            @Override
            public void onItemClick(int position) {
                AppUtils.showToast(ViewActivity0630.this,position+"",true);
            }

            @Override
            public void onCheck(boolean ischeck, int position) {
                AppUtils.showToast(ViewActivity0630.this,position+""+ischeck,true);
            }
        });

        adapter2.setItemClickListener(new MyGridRecyclerViewAdapter.ItemClickLisener() {
            @Override
            public void onItemClick(int position) {
                AppUtils.showToast(ViewActivity0630.this,position+"",true);
            }

            @Override
            public void onCheck(boolean ischeck, int position) {
                AppUtils.showToast(ViewActivity0630.this,position+""+ischeck,true);
            }
        });
    }

    /**
     * 根据数据源的格式与需求设置合适的新数据
     */
    private List<GridDataModle> getNewList(List<Object> list) {
        List<GridDataModle> list_new = new ArrayList<>();

        if (list.size() >= 5) {
            // 最后一行3个
            if (list.size() % 3 == 0) {
                for (Object s : list) {
                    list_new.add(new GridDataModle(s, 2));
                }
            }
            // 最后一行1个
            if (list.size() % 3 == 1) {
                if (list.size() >= 5) {
                    // 到最后一个之前都是每个item占据1/3的行宽度
                    for (int y = 0; y < list.size(); y++) {
                        if (y < list.size() - 1) {
                            list_new.add(new GridDataModle(list.get(y), 2));
                        } else {
                            list_new.add(new GridDataModle(list.get(y), 4));
                        }
                    }
                }
            }
            // 最后一行2个
            if (list.size() % 3 == 2) {
                for (int y = 0; y < list.size(); y++) {
                    if (y < list.size() - 2) {
                        list_new.add(new GridDataModle(list.get(y), 2));
                    } else {
                        list_new.add(new GridDataModle(list.get(y), 3));
                    }
                }
            }
        } else {
            if (list.size() == 1) {
                list_new.add(new GridDataModle(list.get(0), 4));
            }
            if (list.size() == 2) {
                list_new.add(new GridDataModle(list.get(0), 4));
                list_new.add(new GridDataModle(list.get(1), 4));
            }
            // 最后一行3个
            if (list.size() == 3) {
                list_new.add(new GridDataModle(list.get(0), 3));
                list_new.add(new GridDataModle(list.get(1), 3));
                list_new.add(new GridDataModle(list.get(2), 4));
            }
            if (list.size() == 4) {
                list_new.add(new GridDataModle(list.get(0), 3));
                list_new.add(new GridDataModle(list.get(1), 3));
                list_new.add(new GridDataModle(list.get(2), 3));
                list_new.add(new GridDataModle(list.get(3), 3));
            }
        }
        return list_new;
    }

//    private class QuickAdapter1 extends BaseQuickAdapter {
//
//        public QuickAdapter1(int i ,List data) {
//            super(i, data);
//        }
//
//        @Override
//        protected void convert(BaseViewHolder baseViewHolder, Object o) {
//            GridDataModle x = (GridDataModle) o;
//            baseViewHolder.setText(R.id.text_name, x.getName() + "")
//                    .setChecked(R.id.btn_toggle, false);
//            baseViewHolder.setOnClickListener(R.id.grid_item_lay_1, new OnItemChildClickListener())
//                    .setOnClickListener(R.id.btn_toggle, new OnItemChildClickListener());
//        }
//    }
//
//    private class QuickAdapter2 extends BaseQuickAdapter {
//
//        public QuickAdapter2(int i,List data) {
//            super(i, data);
//        }
//
//        @Override
//        protected void convert(BaseViewHolder baseViewHolder, Object o) {
//            GridDataModle x = (GridDataModle) o;
//            baseViewHolder.setText(R.id.text_grid_name, x.getName() + "")
//                    .setChecked(R.id.grid_check, false);
//            baseViewHolder.setOnClickListener(R.id.grid_item_lay_2, new OnItemChildClickListener())
//                    .setOnClickListener(R.id.grid_check, new OnItemChildClickListener());
//        }
//    }

}
